//
//  LFUCacheTests.m
//  LFUCacheTests
//
//  Created by caixiang305621856 on 08/14/2021.
//  Copyright (c) 2021 caixiang305621856. All rights reserved.
//
#import "ACMemoryCache.h"
@import XCTest;

@interface Tests : XCTestCase

@property (nonatomic, strong)ACMemoryCache *cache;

@end

@implementation Tests

- (void)setUp
{
    [super setUp];
    self.cache = [[ACMemoryCache alloc] initWithName:@"AC_Cache"];
    self.cache.countLimit = 3;
    self.cache.costLimit = 100;
    // Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown
{
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
    self.cache = nil;
}

- (void)testInsertion
{
    [self.cache setObject:@1 forKey:@"foo" cost:1];
    [self.cache setObject:@2 forKey:@"bar" cost:2];
    [self.cache setObject:@3 forKey:@"baz" cost:3];
    XCTAssertEqual([self.cache totalCount], 3, @"Insertion failed");
    XCTAssertEqual([self.cache totalCost], 6, @"Insertion failed");
}

- (void)testRemoval
{
    [self.cache setObject:@1 forKey:@"foo" cost:1];
    [self.cache setObject:@2 forKey:@"bar" cost:2];
    [self.cache setObject:@3 forKey:@"baz" cost:3];
    
    [self.cache removeObjectForKey:@"bar"];
    
    XCTAssertEqual([self.cache totalCount], 2, @"Removal failed");
    XCTAssertNil([self.cache objectForKey:@"bar"], @"Removal failed");
}

- (void)testCountEviction
{
    [self.cache setObject:@1 forKey:@"foo"];
    [self.cache setObject:@2 forKey:@"bar"];
    [self.cache setObject:@3 forKey:@"baz"];
    [self.cache setObject:@4 forKey:@"bam"];
    
    XCTAssertEqual([self.cache totalCount], 3, @"Eviction failed");
    XCTAssertNil([self.cache objectForKey:@"foo"], @"Eviction failed");
    
    [self.cache setObject:@5 forKey:@"boo"];
    
    XCTAssertEqual([self.cache totalCount], 3, @"Eviction failed");
    XCTAssertNil([self.cache objectForKey:@"bar"], @"Eviction failed");
}

- (void)testCostEviction
{
    [self.cache setObject:@1 forKey:@"foo" cost:99];
    [self.cache setObject:@2 forKey:@"bar" cost:2];
    
    XCTAssertEqual([self.cache totalCount], 1, @"Eviction failed");
    XCTAssertEqual([self.cache totalCost], 2, @"Eviction failed");
    XCTAssertNil([self.cache objectForKey:@"foo"], @"Eviction failed");
    
    [self.cache setObject:@3 forKey:@"baz" cost:999];
    
    XCTAssertEqual([self.cache totalCount], 0, @"Eviction failed");
    XCTAssertEqual([self.cache totalCost], 0, @"Eviction failed");
}

- (void)testCleanup
{
    [self.cache setObject:@1 forKey:@"foo"];
    [self.cache setObject:@2 forKey:@"bar"];
    [self.cache setObject:@3 forKey:@"baz"];
    //simulate memory warning
    [self.cache removeAllObjects];
    
    XCTAssertEqual([self.cache totalCount], 0, @"Cleanup failed");
    XCTAssertEqual([self.cache totalCost], 0, @"Cleanup failed");
}


@end

